package c_008;

import java.util.concurrent.locks.ReentrantLock;

/**
 * reentrantlock可以指定为公平锁(先来先得,会先检查队列内有无其他线程在等待)
 */
public class T05_ReentrantLock1 extends Thread {

    private static ReentrantLock lock = new ReentrantLock(true);

    public void run(){
        for(int i = 0; i < 100; i++){
            lock.lock();
            try{
                System.out.println(Thread.currentThread().getName() + "获得锁");
            }finally {
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        T05_ReentrantLock1 rl = new T05_ReentrantLock1();
        Thread t1 = new Thread(rl);
        Thread t2 = new Thread(rl);
        t1.start();
        t2.start();
    }

}

